﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace HslCommunication.BasicFramework
{
	/// <summary>
	/// 数据库操作的相关类，包含了常用的方法，避免大量的重复代码
	/// </summary>
	// Token: 0x0200019E RID: 414
	public static class SoftSqlOperate
	{
		/// <summary>
		/// 普通的执行SQL语句，并返回影响行数，该方法应该放到try-catch代码块中
		/// </summary>
		/// <param name="conStr">数据库的连接字符串</param>
		/// <param name="cmdStr">sql语句，适合插入，更新，删除</param>
		/// <returns>返回受影响的行数</returns>
		/// <exception cref="T:System.Data.SqlClient.SqlException"></exception>
		// Token: 0x06002108 RID: 8456 RVA: 0x000A0A88 File Offset: 0x0009EC88
		public static int ExecuteSql(string conStr, string cmdStr)
		{
			int result;
			using (SqlConnection sqlConnection = new SqlConnection(conStr))
			{
				sqlConnection.Open();
				result = SoftSqlOperate.ExecuteSql(sqlConnection, cmdStr);
			}
			return result;
		}

		/// <summary>
		/// 普通的执行SQL语句，并返回影响行数，该方法应该放到try-catch代码块中
		/// </summary>
		/// <param name="conn">数据库的连接对象</param>
		/// <param name="cmdStr">sql语句，适合插入，更新，删除</param>
		/// <returns>返回受影响的行数</returns>
		/// <exception cref="T:System.Data.SqlClient.SqlException"></exception>
		// Token: 0x06002109 RID: 8457 RVA: 0x000A0ACC File Offset: 0x0009ECCC
		public static int ExecuteSql(SqlConnection conn, string cmdStr)
		{
			int result;
			using (SqlCommand sqlCommand = new SqlCommand(cmdStr, conn))
			{
				result = sqlCommand.ExecuteNonQuery();
			}
			return result;
		}

		/// <summary>
		/// 选择数据表的执行SQL语句，并返回最终数据表，该方法应该放到try-catch代码块中
		/// </summary>
		/// <param name="conStr">数据库的连接字符串</param>
		/// <param name="cmdStr">sql语句，选择数据表的语句</param>
		/// <returns>结果数据表</returns>
		/// <exception cref="T:System.Data.SqlClient.SqlException"></exception>
		/// <exception cref="T:System.InvalidOperationException"></exception>
		// Token: 0x0600210A RID: 8458 RVA: 0x000A0B08 File Offset: 0x0009ED08
		public static DataTable ExecuteSelectTable(string conStr, string cmdStr)
		{
			DataTable result;
			using (SqlConnection sqlConnection = new SqlConnection(conStr))
			{
				sqlConnection.Open();
				result = SoftSqlOperate.ExecuteSelectTable(sqlConnection, cmdStr);
			}
			return result;
		}

		/// <summary>
		/// 选择数据表的执行SQL语句，并返回最终数据表，该方法应该放到try-catch代码块中
		/// </summary>
		/// <param name="conn">数据库连接对象</param>
		/// <param name="cmdStr">sql语句，选择数据表的语句</param>
		/// <returns>结果数据表</returns>
		/// <exception cref="T:System.Data.SqlClient.SqlException"></exception>
		// Token: 0x0600210B RID: 8459 RVA: 0x000A0B4C File Offset: 0x0009ED4C
		public static DataTable ExecuteSelectTable(SqlConnection conn, string cmdStr)
		{
			DataTable result;
			using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdStr, conn))
			{
				using (DataSet dataSet = new DataSet())
				{
					sqlDataAdapter.Fill(dataSet);
					result = dataSet.Tables[0];
				}
			}
			return result;
		}

		/// <summary>
		/// 选择指定类型数据集合执行SQL语句，并返回指定类型的数据集合，该方法应该放到try-catch代码块中
		/// </summary>
		/// <param name="conStr">数据库的连接字符串</param>
		/// <param name="cmdStr">sql语句，选择数据表的语句</param>
		/// <returns>结果数据集合</returns>
		/// <exception cref="T:System.Data.SqlClient.SqlException"></exception>
		/// <exception cref="T:System.InvalidOperationException"></exception>
		// Token: 0x0600210C RID: 8460 RVA: 0x000A0BB4 File Offset: 0x0009EDB4
		public static List<T> ExecuteSelectEnumerable<T>(string conStr, string cmdStr) where T : ISqlDataType, new()
		{
			List<T> result;
			using (SqlConnection sqlConnection = new SqlConnection(conStr))
			{
				sqlConnection.Open();
				result = SoftSqlOperate.ExecuteSelectEnumerable<T>(sqlConnection, cmdStr);
			}
			return result;
		}

		/// <summary>
		/// 选择指定类型数据集合执行SQL语句，并返回指定类型的数据集合，该方法应该放到try-catch代码块中
		/// </summary>
		/// <param name="conn">数据库的连接对象</param>
		/// <param name="cmdStr">sql语句，选择数据表的语句</param>
		/// <returns>结果数据集合</returns>
		/// <exception cref="T:System.Data.SqlClient.SqlException"></exception>
		/// <exception cref="T:System.InvalidOperationException"></exception>
		// Token: 0x0600210D RID: 8461 RVA: 0x000A0BF8 File Offset: 0x0009EDF8
		public static List<T> ExecuteSelectEnumerable<T>(SqlConnection conn, string cmdStr) where T : ISqlDataType, new()
		{
			List<T> result;
			using (SqlCommand sqlCommand = new SqlCommand(cmdStr, conn))
			{
				using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
				{
					List<T> list = new List<T>();
					while (sqlDataReader.Read())
					{
						T item = Activator.CreateInstance<T>();
						item.LoadBySqlDataReader(sqlDataReader);
						list.Add(item);
					}
					result = list;
				}
			}
			return result;
		}

		/// <summary>
		/// 更新指定类型数据执行SQL语句，并返回指定类型的数据集合，该方法应该放到try-catch代码块中
		/// </summary>
		/// <param name="conStr">数据库的连接字符串</param>
		/// <param name="cmdStr">sql语句，选择数据表的语句</param>
		/// <returns>结果数据</returns>
		/// <exception cref="T:System.Data.SqlClient.SqlException"></exception>
		/// <exception cref="T:System.InvalidOperationException"></exception>
		// Token: 0x0600210E RID: 8462 RVA: 0x000A0C84 File Offset: 0x0009EE84
		public static T ExecuteSelectObject<T>(string conStr, string cmdStr) where T : ISqlDataType, new()
		{
			T result;
			using (SqlConnection sqlConnection = new SqlConnection(conStr))
			{
				sqlConnection.Open();
				result = SoftSqlOperate.ExecuteSelectObject<T>(sqlConnection, cmdStr);
			}
			return result;
		}

		/// <summary>
		/// 更新指定类型数据执行SQL语句，并返回指定类型的数据集合，该方法应该放到try-catch代码块中
		/// </summary>
		/// <param name="conn">数据库的连接对象</param>
		/// <param name="cmdStr">sql语句，选择数据表的语句</param>
		/// <returns>结果数据</returns>
		/// <exception cref="T:System.Data.SqlClient.SqlException"></exception>
		/// <exception cref="T:System.InvalidOperationException"></exception>
		// Token: 0x0600210F RID: 8463 RVA: 0x000A0CC8 File Offset: 0x0009EEC8
		public static T ExecuteSelectObject<T>(SqlConnection conn, string cmdStr) where T : ISqlDataType, new()
		{
			T result;
			using (SqlCommand sqlCommand = new SqlCommand(cmdStr, conn))
			{
				using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
				{
					bool flag = sqlDataReader.Read();
					if (flag)
					{
						T t = Activator.CreateInstance<T>();
						t.LoadBySqlDataReader(sqlDataReader);
						result = t;
					}
					else
					{
						result = default(T);
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 用于选择聚合函数值的方法，例如Count，Average，Max，Min，Sum等最终只有一个结果值的对象
		/// </summary>
		/// <param name="conStr">数据库的连接字符串</param>
		/// <param name="cmdStr">sql语句，选择数据表的语句</param>
		/// <returns>返回的int数据</returns>
		// Token: 0x06002110 RID: 8464 RVA: 0x000A0D50 File Offset: 0x0009EF50
		public static int ExecuteSelectCount(string conStr, string cmdStr)
		{
			int result;
			using (SqlConnection sqlConnection = new SqlConnection(conStr))
			{
				sqlConnection.Open();
				result = SoftSqlOperate.ExecuteSelectCount(sqlConnection, cmdStr);
			}
			return result;
		}

		/// <summary>
		/// 用于选择聚合函数值的方法，例如Count，Average，Max，Min，Sum等最终只有一个结果值的对象
		/// </summary>
		/// <param name="conn">数据库的连接对象</param>
		/// <param name="cmdStr">sql语句，选择数据表的语句</param>
		/// <returns>返回的int数据</returns>
		// Token: 0x06002111 RID: 8465 RVA: 0x000A0D94 File Offset: 0x0009EF94
		public static int ExecuteSelectCount(SqlConnection conn, string cmdStr)
		{
			int result;
			using (SqlCommand sqlCommand = new SqlCommand(cmdStr, conn))
			{
				int num = 0;
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				bool flag = sqlDataReader.Read();
				if (flag)
				{
					num = Convert.ToInt32(sqlDataReader[0]);
				}
				sqlDataReader.Close();
				result = num;
			}
			return result;
		}
	}
}
